﻿using BtLabelTool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using BtLabelModel;
using Newtonsoft.Json;
using System.Net;
using System.IO;

namespace BtLabelDAL
{
    public class HttpDAL
    {
        public HttpModel.GetLabelInfoRb GetLabelInfo(string mac)
        {
            try
            {
                var dic = ConfigHelper.ReadAll();
                string infoUrl = dic["InfoUrl"];
                using (var httpClient = new HttpClient())
                {
                    //get
                    var url = new Uri(infoUrl + mac);
                    // response
                    var response = httpClient.GetAsync(url).Result;
                    var data = response.Content.ReadAsStringAsync().Result;
                    //todo
                    return JsonConvert.DeserializeObject<HttpModel.GetLabelInfoRb>(data);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public bool HttpPostRefresh(HttpModel.RefreshLabelDto dto)
        {
            var dic = ConfigHelper.ReadAll();
            string responseContent = "";
            try
            {
                HttpWebRequest request = WebRequest.Create(dic["RefreshUrl"]) as HttpWebRequest;
                request.Method = "POST";
                request.ContentType = "application/json;charset=UTF-8";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                request.KeepAlive = true;
                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    streamWriter.Write(JsonConvert.SerializeObject(dto));
                    streamWriter.Flush();
                    streamWriter.Close();
                }
                HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse();
                StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
                responseContent = streamReader.ReadToEnd();
                httpWebResponse.Close();
                streamReader.Close();
                if (!responseContent.Contains("success"))
                {
                    return false;
                }
                return true;
            }

            catch (Exception ex)
            {
                throw ex;
            }

        }
        public bool HttpPostLed(HttpModel.LightLedDto dto)
        {
            var dic = ConfigHelper.ReadAll();
            string responseContent = "";
            try
            {
                HttpWebRequest request = WebRequest.Create(dic["LightUrl"]) as HttpWebRequest;
                request.Method = "POST";
                request.ContentType = "application/json;charset=UTF-8";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                request.KeepAlive = true;
                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    streamWriter.Write(JsonConvert.SerializeObject(dto));
                    streamWriter.Flush();
                    streamWriter.Close();
                }
                HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse();
                StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
                responseContent = streamReader.ReadToEnd();
                httpWebResponse.Close();
                streamReader.Close();
                if (!responseContent.Contains("success"))
                {
                    return false;
                }
                return true;
            }

            catch (Exception ex)
            {
                throw ex;
            }

        }

        public static string HttpPost(string url,string json)
        {
            var dic = ConfigHelper.ReadAll();
            string responseContent = "";
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.Method = "POST";
                request.ServicePoint.Expect100Continue = false;
                request.AutomaticDecompression = DecompressionMethods.GZip;
                request.ContentType = "application/json;charset=UTF-8";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                request.AllowAutoRedirect = true;
                request.KeepAlive = true;
                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    streamWriter.Write(json);
                    streamWriter.Flush();
                    streamWriter.Close();
                }
                HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse();
                StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
                responseContent = streamReader.ReadToEnd();
                httpWebResponse.Close();
                streamReader.Close();
                return responseContent;
            }

            catch (Exception ex)
            {
                throw ex;
            }

        }
    }
}
